// 操作数据库暴露出来的接口  

// 获取mongoose
const mongoose = require("mongoose");
//get 列表数据
const get = async ({
    curPage,
    eachPage
}) => {
    // 强制转换类型 number转string
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    }
    // option
    //mongoose.model传三个参数是创建  传一个参数是获取
    const theatersModel = mongoose.model("theaters");
    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await theatersModel.countDocuments();
    // Math.ceil往上取整
    data.maxPage = Math.ceil(data.total / eachPage);
    // find查询所有数据 findOne查询满意结果的第一条
    // skip跳过多少条开始查找 
    //limit 查询多少条数据
    // sort 排序
    //exec 立即执行
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await theatersModel.find().populate('cinemasId').skip(skipNum).limit(eachPage).exec()
    return data;
};

const getCinemas = async () => {
    //拿到cinemas这个model
    const theatersModel = mongoose.model("cinemas");
    // find查询所有数据
    data = await theatersModel.find().exec()
    return data;
};
// 新增影院信息
const addTheaters = async ({
    cinemasId,
    theatersName,
    theatersStatus
}) => {
    //拿到theaters这个model
    const theatersModel = mongoose.model("theaters");
    // 查找影院里面有没有新增的放映厅名字
    const data = await theatersModel.findOne({cinemasId,
        theatersName
    })

    if (data) {
        return false
    } else {
        return await theatersModel.create({
            cinemasId,
            theatersName,
            theatersStatus
        })
    };
}
// 删除座位
const delSeats = async ({
    _id
}) => {
    //拿到theaters这个model
    const theatersModel = mongoose.model("theaters");
    const seatsModel = mongoose.model("seats");
    // 先查找传入进来的放映厅在不在
    const theatersNum = await theatersModel.find({
        _id
    })
    if (theatersNum) {
        await theatersModel.remove({
            _id
        })
        await seatsModel.remove({
            theaterId: _id
        })
        return true;
    } else {
        return false;
    }
    // 删除数据 传入参数是id
};
// 获取一条影院的数据接受参数是一个id
const getOne = async ({
    _id
}) => {
    //拿到theaters这个model
    const theatersModel = mongoose.model("theaters");
    // 先查找传入进来的放映厅在不在
    return await theatersModel.findOne({
        _id
    })

};
// 获取一条影院的数据接受参数是一个id
const changeOne = async ({
    _id,
    cinemasId,
    theatersName,
    theatersStatus
}) => {
    //拿到theaters这个model
    const theatersModel = mongoose.model("theaters");
    // 先查找传入进来的放映厅在不在
    const status = await theatersModel.updateOne({
        _id
    }, {
        cinemasId,
        theatersName,
        theatersStatus
    });
    return status

};
// 查询所有数据信息
const getAllInfo = async () => {
    //拿到theaters这个model
    const theatersModel = mongoose.model("theaters");
    // 先查找影厅模块的所有数据
    return await theatersModel.find().populate('cinemasId')
};

// 查询某个字段的值
const select = async (enterData) => {
    // 创建一个要放入find实参的对象
    //拿到theaters这个model
    const theatersModel = mongoose.model("theaters");
    // 获取到 这个字段值对应的所有数据
    return await theatersModel.find(enterData).populate('cinemasId')
};

// 新增座位
const creatSeats = async ({
    _id
}) => {
    const seatsModel = mongoose.model("seats");
    const seatsArr = []
    for (let i = 1; i <= 9; i++) {
        for (let j = 1; j <= 9; j++) {
            seatsArr.push({
                displayName: `${i}排${j}列`,
                theaterId: _id
            })
        }
    }
    return await seatsModel.create(seatsArr)
};
module.exports = {
    get,
    getCinemas,
    addTheaters,
    delSeats,
    getOne,
    changeOne,
    getAllInfo,
    select,
    creatSeats
}